package member.member.action;

import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.common.model.util.ModelQueryCheck;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.system.util.JSONMessage;
import member.base.model.MemberInfo;
import member.base.model.StatusModel;

@ActionRequest(name = "action/member/memberInfo",description = "会员")
public class MemberInfoAction extends MemberAction{
    @RequestMethod(description = "登录信息")
    public JSONMessage loginInfo(@Param("oid") String oid) throws Exception {
        MemberInfo login=getSession(false);
        if(null!=login&&oid.equals(login.getOid())) return JSONMessage.success("").push("login",login);
        MemberInfo member= ModelQueryList.getModel(MemberInfo.class,oid,"realName","phoneNum","business.name","business.description");
        if(null!=member) {
            return JSONMessage.success("")
                .push("login", new JSONMessage()
                    .push("realName", member.getRealName().substring(0, 1))
                    .push("phoneNum", member.getPhoneNum().substring(0, 3))
                    .push("business", new JSONMessage()
                        .push("name", member.getBusiness().getName())
                        .push("description", member.getBusiness().getDescription())
                    )
                );
        }else{
            return JSONMessage.error("会员信息不存在");
        }
    }
    @RequestMethod(description = "登录会员")
    public JSONMessage doLogin(@Param("login") MemberInfo login) throws Exception {
        ModelQueryCheck.init(login)
            .checkNotNull("realName","姓名不能为空")
            .checkNotNull("phoneNum","手机号不能为空")
            .doCheck(false,true);
        MemberInfo model=ModelQueryList.getModel(MemberInfo.class,new String[]{"oid"},
            QueryCondition.and(
                QueryCondition.eq("realName",login.getRealName()),
                QueryCondition.eq("phoneNum",login.getPhoneNum()),
                QueryCondition.eq("status", StatusModel.StatusEnabled)
            )
        );
        if(null!=model){
            setSession(model);
            return JSONMessage.success("登录成功");
        }else{
            return JSONMessage.error("会员信息错误或已停用");
        }
    }
}
